# add java (.java, and .class), and antlr (.g, and .tokens) extensions
# to the list of known file extentions
.SUFFIXES: .java .class

# generic 
JAVAC = javac
JAR = jar
JAVA = java
CLASSPATH = ../lib/antlrworks-1.4.jar:.
JFLAGS = -classpath $(CLASSPATH)
ANTLR_TOOL = org.antlr.Tool

# compile a .java file into one or more .class files
.java.class:
	$(JAVAC) $(JFLAGS) $<


# default target is all
all: build


#
K = k
K_LEXER_GRAMMAR = $(K).g
K_TOKENS = $(K).tokens
K_LEXER = $(K).java
K_GENERATED = $(K_TOKENS) $(K_LEXER)

ANNOT = annot
ANNOT_GRAMMAR = $(ANNOT).g
ANNOT_TOKENS = $(ANNOT).tokens
ANNOT_LEXER = $(ANNOT)Lexer.java
ANNOT_PARSER = $(ANNOT)Parser.java
ANNOT_TREE_GRAMMAR_1 = $(ANNOT)Pass1.g
ANNOT_TOKENS_1 = $(ANNOT)Pass1.tokens
ANNOT_WALKER_1 = $(ANNOT)Pass1.java
ANNOT_TREE_GRAMMAR_2 = $(ANNOT)Pass2.g
ANNOT_TOKENS_2 = $(ANNOT)Pass2.tokens
ANNOT_WALKER_2 = $(ANNOT)Pass2.java
ANNOT_TREE_GRAMMAR_3 = $(ANNOT)Pass3.g
ANNOT_TOKENS_3 = $(ANNOT)Pass3.tokens
ANNOT_WALKER_3 = $(ANNOT)Pass3.java
ANNOT_TREE_GRAMMAR_4 = $(ANNOT)Pass4.g
ANNOT_TOKENS_4 = $(ANNOT)Pass4.tokens
ANNOT_WALKER_4 = $(ANNOT)Pass4.java
ANNOT_TREE_GRAMMAR_5 = $(ANNOT)Pass5.g
ANNOT_TOKENS_5 = $(ANNOT)Pass5.tokens
ANNOT_WALKER_5 = $(ANNOT)Pass5.java
ANNOT_GENERATED_JAVA = $(ANNOT_LEXER) $(ANNOT_PARSER) \
                       $(ANNOT_WALKER_1) $(ANNOT_WALKER_2) $(ANNOT_WALKER_3) \
                       $(ANNOT_WALKER_4) $(ANNOT_WALKER_5)
ANNOT_GENERATED = $(ANNOT_GENERATED_JAVA) $(ANNOT_TOKENS) \
                  $(ANNOT_TOKENS_1) $(ANNOT_TOKENS_2) $(ANNOT_TOKENS_3) \
                  $(ANNOT_TOKENS_4) $(ANNOT_TOKENS_5)
ANNOT_MAIN = AnnotPreK.java

KERNELC = kernelC
KERNELC_GRAMMAR = $(KERNELC).g
KERNELC_TOKENS = $(KERNELC).tokens
KERNELC_LEXER = $(KERNELC)Lexer.java
KERNELC_PARSER = $(KERNELC)Parser.java
KERNELC_TREE_GRAMMAR_1 = $(KERNELC)Pass1.g
KERNELC_TOKENS_1 = $(KERNELC)Pass1.tokens
KERNELC_WALKER_1 = $(KERNELC)Pass1.java
KERNELC_TREE_GRAMMAR_2 = $(KERNELC)Pass2.g
KERNELC_TOKENS_2 = $(KERNELC)Pass2.tokens
KERNELC_WALKER_2 = $(KERNELC)Pass2.java
KERNELC_TREE_GRAMMAR_3 = $(KERNELC)Pass3.g
KERNELC_TOKENS_3 = $(KERNELC)Pass3.tokens
KERNELC_GENERATED_JAVA = $(KERNELC_LEXER) $(KERNELC_PARSER) \
                         $(KERNELC_WALKER_1) $(KERNELC_WALKER_2)
KERNELC_GENERATED = $(KERNELC_GENERATED_JAVA) $(KERNELC_TOKENS) \
                    $(KERNELC_TOKENS_1) $(KERNELC_TOKENS_2) $(KERNELC_TOKENS_3)
KERNELC_MAIN = KernelCPreK.java


# make k tokens file
$(K_TOKENS) $(K_LEXER): $(K_LEXER_GRAMMAR)
	$(JAVA) $(JFLAGS) $(ANTLR_TOOL) $(K_LEXER_GRAMMAR)


# make matching logic annotation parser
$(ANNOT_TOKENS) $(ANNOT_LEXER) $(ANNOT_PARSER): $(ANNOT_GRAMMAR) $(K_TOKENS)
	$(JAVA) $(JFLAGS) $(ANTLR_TOOL) $(ANNOT_GRAMMAR)

$(ANNOT_TOKENS_1) $(ANNOT_WALKER_1): $(ANNOT_TREE_GRAMMAR_1) $(ANNOT_TOKENS)
	$(JAVA) $(JFLAGS) $(ANTLR_TOOL) $(ANNOT_TREE_GRAMMAR_1)

$(ANNOT_TOKENS_2) $(ANNOT_WALKER_2): $(ANNOT_TREE_GRAMMAR_2) $(ANNOT_TOKENS)
	$(JAVA) $(JFLAGS) $(ANTLR_TOOL) $(ANNOT_TREE_GRAMMAR_2)

$(ANNOT_TOKENS_3) $(ANNOT_WALKER_3): $(ANNOT_TREE_GRAMMAR_3) $(ANNOT_TOKENS)
	$(JAVA) $(JFLAGS) $(ANTLR_TOOL) $(ANNOT_TREE_GRAMMAR_3)

$(ANNOT_TOKENS_4) $(ANNOT_WALKER_4): $(ANNOT_TREE_GRAMMAR_4) $(ANNOT_TOKENS)
	$(JAVA) $(JFLAGS) $(ANTLR_TOOL) $(ANNOT_TREE_GRAMMAR_4)

$(ANNOT_TOKENS_5) $(ANNOT_WALKER_5): $(ANNOT_TREE_GRAMMAR_5) $(ANNOT_TOKENS)
	$(JAVA) $(JFLAGS) $(ANTLR_TOOL) $(ANNOT_TREE_GRAMMAR_5)


# make kernelc parser
$(KERNELC_TOKENS) $(KERNELC_LEXER) $(KERNELC_PARSER): $(KERNELC_GRAMMAR) \
                                                      $(K_TOKENS)
	$(JAVA) $(JFLAGS) $(ANTLR_TOOL) $(KERNELC_GRAMMAR)

$(KERNELC_WALKER_1) $(KERNELC_TOKENS_1): $(KERNELC_TREE_GRAMMAR_1) \
                                         $(KERNELC_TOKENS)
	$(JAVA) $(JFLAGS) $(ANTLR_TOOL) $(KERNELC_TREE_GRAMMAR_1)

$(KERNELC_WALKER_2) $(KERNELC_TOKENS_2): $(KERNELC_TREE_GRAMMAR_2) \
                                         $(KERNELC_TOKENS)
	$(JAVA) $(JFLAGS) $(ANTLR_TOOL) $(KERNELC_TREE_GRAMMAR_2)


# make java classes
JAVA_SOURCES = Table.java $(K_LEXER) TreeUtils.java \
               $(ANNOT_GENERATED_JAVA) $(ANNOT_MAIN) \
               $(KERNELC_GENERATED_JAVA) $(KERNELC_MAIN)
CLASSES = $(JAVA_SOURCES:.java=.class)
PARSER_JAR = matchCparser.jar
MAIN = $(KERNELC_MAIN:.java=)


# targets
# build target
build: $(PARSER_JAR)

# build a jar
$(PARSER_JAR): $(CLASSES)
#$(PARSER_JAR): $(JAVA_SOURCES)
#	$(JAVAC) $(JFLAGS) $(JAVA_SOURCES)
	$(JAR) -cf $@ *.class

# run target (depends on build)
run: build
	$(JAVA) $(JFLAGS) $(MAIN)

# clean target removes the .class files and the files generated by antlr
clean:
	rm -rf $(K_GENERATED) $(ANNOT_GENERATED) $(KERNELC_GENERATED)
	rm -rf $(PARSER_JAR) *.class

